import org.openqa.selenium.*;
import com.daxiang.core.testng.TestCaseTestListener;
import io.appium.java_client.pagefactory.*;
import com.daxiang.core.pc.web.BrowserDevice;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.*;
import com.daxiang.core.Device;
import com.daxiang.core.testng.DebugActionTestListener;
import org.testng.annotations.*;
import org.testng.SkipException;
import com.daxiang.core.DeviceHolder;
import com.daxiang.action.PCWebAction;
import java.util.*;
import static org.assertj.core.api.Assertions.*;

public class Demo {

    private String deviceId = "4d905e0b65be4634be8f9db80ad7df6c";
    private Device device = DeviceHolder.get(deviceId);
    private PCWebAction $ = new PCWebAction((BrowserDevice) device);
    private RemoteWebDriver driver = (RemoteWebDriver) device.getDriver();
    private Map<String, Object> vars = new HashMap();

    public static final String gVerifyCode = "123456"; // 默认验证码
    @FindBy(xpath = "//*[@id=\"root\"]/div/div/div/div[3]/div[2]/div/div/div/div[1]/input")
    private WebElement loginPage_accountInput; // 手机号文本框
    @FindBy(xpath = "//*[@id=\"root\"]/div/div/div/div[3]/div[2]/div/div/div/div[2]/input")
    private WebElement loginPage_passwordInput; // 密码文本框
    @FindBy(xpath = "//*[text()=\"立即登录\"]")
    private WebElement loginPage_loginBtn; // 登录按钮
    @FindBy(xpath = "//*[text()=\"手机验证码登录\"]")
    private WebElement loginPage_loginByCodeBtn; // 切换验证码登录按钮
    @FindBy(xpath = "//*[text()=\"获取验证码\"]")
    private WebElement loginPage_getCodeBtn; // 获取验证码按钮
    @FindBy(xpath = "//*[@id=\"root\"]/div/div/div/div[3]/div[2]/div/div/div/div[2]/input")
    private WebElement loginPage_codeInput; // 验证码文本框
    @FindBy(xpath = "//*[text()=\"密码登录\"]")
    private WebElement loginPage_loginByPasswordBtn; // 切换密码登录按钮

    @FindBy(xpath = "//*[text()=\"密码登录\"]")
    private By loginPage_loginBy;

    @BeforeSuite
    public void beforeSuite() throws Throwable {
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    @Test
    public void testcase_0() throws Throwable {
        action_0();
    }

    // loginDemo
    public void action_0() throws Throwable {
        // step1.验证码登录
        action_10006("15010000003", gVerifyCode);
        action_logout();
    }

    // loginByCode
    public void action_10006(String account, String code) throws Throwable {
        // step1.设置隐式等待时间
        action_11(3);
        // step2.切换验证码登录
        loginPage_loginByCodeBtn.click();
        // step3.输入账号
        loginPage_accountInput.sendKeys(Keys.CONTROL, "a");
        loginPage_accountInput.sendKeys(account);
        // step4.获取验证码
        loginPage_getCodeBtn.click();
        // step5.输入验证码
        loginPage_codeInput.sendKeys(Keys.CONTROL, "a");
        loginPage_codeInput.sendKeys(code);
        // step6.点击登录
        loginPage_loginBtn.click();
    }

    public void action_logout() {
        Actions actions = new Actions(driver);
        // 鼠标移动到元素位置
        actions.moveToElement(loginPage_accountInput);
    }

    // 设置隐式等待时间
    public void action_11(long seconds) throws Throwable {
        $.setImplicitlyWaitTime(seconds);
    }

    private void freshDriver() {
        driver = (RemoteWebDriver) device.freshDriver();
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    private void print(Object o) {
        DebugActionTestListener.addPrintMsg(String.valueOf(o));
    }
}
